package com.itheima.service.pmsService.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.pms.PmsProductAttribute;
import com.itheima.domain.db.pms.PmsProductAttributeCategory;
import com.itheima.domain.db.pms.PmsProductCategory;
import com.itheima.mapper.pmsMapper.PmsProductCategoryMapper;
import com.itheima.mapper.pmsMapper.ProductAttributeCategoryMapper;
import com.itheima.mapper.pmsMapper.ProductAttributeMapper;
import com.itheima.service.pmsService.ProductAttributeCategoryService;
import com.itheima.vo.pms.ProductAttributeCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class ProductAttributeCategorysServiceImpl implements ProductAttributeCategoryService {

    @Autowired
    ProductAttributeCategoryMapper productAttributeCategoryMapper;

    @Autowired
    ProductAttributeMapper productAttributeMapper;

    @Autowired
    PmsProductCategoryMapper pmsProductCategoryMapper;


    @Override
    public void add(PmsProductAttributeCategory productAttributeCategory) {
        productAttributeCategoryMapper.insert(productAttributeCategory);
    }

    @Override
    public void delete(Integer id) {
        productAttributeCategoryMapper.deleteById(id);
    }

    @Override
    public PageBean<PmsProductAttributeCategory> getPageList(Integer pageNum, Integer pageSize) {

        IPage<PmsProductAttributeCategory> page = new Page<>(pageNum,pageSize);
        IPage<PmsProductAttributeCategory> iPage = productAttributeCategoryMapper.selectPage(page, null);


        return new PageBean<>(iPage);
    }

    @Override
    public List<ProductAttributeCategoryVo> categoryListWithAttr() {

        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(PmsProductAttribute::getId);
        List<PmsProductAttribute> categoryList = productAttributeMapper.selectList(wrapper);
        List<ProductAttributeCategoryVo> list = new ArrayList<>();
        for (PmsProductAttribute productAttributeCategory : categoryList) {
            ProductAttributeCategoryVo vo = new ProductAttributeCategoryVo();
            BeanUtil.copyProperties(productAttributeCategory,vo);
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,productAttributeCategory.getId());
            List<PmsProductAttribute> productAttributeList = productAttributeMapper.selectList(queryWrapper);
            vo.setProductAttributeList(productAttributeList);
            list.add(vo);
        }
        return list;

    }

    @Override
    public void updateProduct(PmsProductCategory productCategory, Integer id) {
        Long pid = Convert.toLong(id);
        LambdaQueryWrapper<PmsProductCategory>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getId,pid);
        pmsProductCategoryMapper.update(productCategory,wrapper);
    }

    @Override
    public PmsProductCategory getProduct(Integer id) {
        Long pid = Convert.toLong(id);
        LambdaQueryWrapper<PmsProductCategory> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getId,pid);
        return pmsProductCategoryMapper.selectOne(wrapper);
    }
}
